package com.yc.utilslib.reader;

import com.yc.utilslib.utils.ByteUtils;

/**
 * 作者：TanTun
 * 时间：2017/6/12
 * 邮箱：32965926@qq.com
 * 描述：Licence定义与管理类
 */

public class Licence {
    private static Licence sInstance = new Licence();

    private Licence() {
        ch = new byte[2];
        mjxt = 0;
        mjxt_sec = 0;
        mjxt_cardtype = 0x00;
        xfxt = 0;
        xfxt_sec = 0;
        jsxt = 0;
        jsxt_sec = 0;
        syscard_no_xf = new byte[5];
        syscard_no_mj = new byte[5];
        syscard_no_js = new byte[5];
        sys_keyb = new byte[8];
        user_password = new byte[8];
        oprater_password = new byte[8];
        comm_password = new byte[8];
        unuser = new byte[6];
        user_keyAB = new byte[16];
        user_return_keyAB = new byte[16];
        unkonw = new byte[8];
        keyB = new byte[6];
    }

    /**
     * 从字节流构造Licence
     *
     * @param data licence数据
     * @return 是否成功
     */
    public static boolean fromData(byte[] data) {
        try {
            if (data.length < 120) return false;
            int pos = 0;

            System.arraycopy(data, pos, sInstance.ch, 0, 2);
            pos += 2;
            sInstance.mjxt = ByteUtils.byteArrayToInt(data, pos, ByteUtils.Endian.Big);
            pos += 4;
            sInstance.mjxt_sec = ByteUtils.byteArrayToInt(data, pos, ByteUtils.Endian.Big);
            pos += 4;
            sInstance.mjxt_cardtype = data[pos];
            pos += 1;
            sInstance.xfxt = ByteUtils.byteArrayToInt(data, pos, ByteUtils.Endian.Big);
            pos += 4;
            sInstance.xfxt_sec = ByteUtils.byteArrayToInt(data, pos, ByteUtils.Endian.Big);
            pos += 4;
            sInstance.jsxt = ByteUtils.byteArrayToInt(data, pos, ByteUtils.Endian.Big);
            pos += 4;
            sInstance.jsxt_sec = ByteUtils.byteArrayToInt(data, pos, ByteUtils.Endian.Big);
            pos += 4;
            System.arraycopy(data, pos, sInstance.syscard_no_xf, 0, 5);
            pos += 5;
            System.arraycopy(data, pos, sInstance.syscard_no_mj, 0, 5);
            pos += 5;
            System.arraycopy(data, pos, sInstance.syscard_no_js, 0, 5);
            pos += 5;
            System.arraycopy(data, pos, sInstance.sys_keyb, 0, 8);
            pos += 8;
            System.arraycopy(data, pos, sInstance.user_password, 0, 8);
            pos += 8;
            System.arraycopy(data, pos, sInstance.oprater_password, 0, 8);
            pos += 8;
            System.arraycopy(data, pos, sInstance.comm_password, 0, 8);
            pos += 8;
            System.arraycopy(data, pos, sInstance.unuser, 0, 6);
            pos += 6;
            System.arraycopy(data, pos, sInstance.user_keyAB, 0, 16);
            pos += 16;
            System.arraycopy(data, pos, sInstance.user_return_keyAB, 0, 16);
            pos += 16;
            System.arraycopy(data, pos, sInstance.unkonw, 0, 8);
            System.arraycopy(sInstance.sys_keyb, 0, sInstance.keyB, 0, 6);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return true;
    }

    public static Licence getInstance() {
        return sInstance;
    }

    private byte[] ch;
    private int mjxt;
    private int mjxt_sec;
    private byte mjxt_cardtype;
    private int xfxt;
    private int xfxt_sec;
    private int jsxt;
    private int jsxt_sec;
    private byte[] syscard_no_xf;
    private byte[] syscard_no_mj;
    private byte[] syscard_no_js;
    private byte[] sys_keyb;
    private byte[] user_password;
    private byte[] oprater_password;
    private byte[] comm_password;
    private byte[] unuser;
    private byte[] user_keyAB;
    private byte[] user_return_keyAB;
    private byte[] unkonw;
    private byte[] keyB;

    public byte[] getCh() {
        return ch;
    }

    public int getMjxt() {
        return mjxt;
    }

    public int getMjxt_sec() {
        return mjxt_sec;
    }

    public byte getMjxt_cardtype() {
        return mjxt_cardtype;
    }

    public int getXfxt() {
        return xfxt;
    }

    public int getXfxt_sec() {
        return xfxt_sec;
    }

    public int getJsxt() {
        return jsxt;
    }

    public int getJsxt_sec() {
        return jsxt_sec;
    }

    public byte[] getSyscard_no_xf() {
        return syscard_no_xf;
    }

    public byte[] getSyscard_no_mj() {
        return syscard_no_mj;
    }

    public byte[] getSyscard_no_js() {
        return syscard_no_js;
    }

    public byte[] getSys_keyb() {
        return sys_keyb;
    }

    public byte[] getUser_password() {
        return user_password;
    }

    public byte[] getOprater_password() {
        return oprater_password;
    }

    public byte[] getComm_password() {
        return comm_password;
    }

    public byte[] getUnuser() {
        return unuser;
    }

    public byte[] getUser_keyAB() {
        return user_keyAB;
    }

    public byte[] getUser_return_keyAB() {
        return user_return_keyAB;
    }

    public byte[] getUnkonw() {
        return unkonw;
    }

    public byte[] getKeyB() {
        return keyB;
    }

    /**
     * 计算用户卡密钥
     *
     * @param snr 物理卡号
     * @return 用户卡密钥
     */
    public byte[] calculateAthKey(byte[] snr) {
        byte[] key = new byte[6];
        key[0]	= (byte)(oprater_password[2] + snr[0] + 0x33);
        key[1]	= (byte)(oprater_password[1] - snr[1] + 0x24);
        key[2]	= (byte)(oprater_password[0] | snr[2] + 0x35);
        key[3]	= (byte)(oprater_password[3] & snr[3] + 0x46);
        key[4]	= (byte)(oprater_password[4] ^ snr[0] + snr[1] + snr[2] + snr[3] + 0x57);
        key[5]	= (byte)(oprater_password[5] ^ snr[0] ^ snr[1] ^ snr[2] ^ snr[3] + 0x68);
        return key;
    }
}
